clear;
%Assignment_A
disp('------------------------Assignment A--------------------------');
figure(1);
nodes=0.99:(1.01-0.99)/100:1.01;
fx=funcf(nodes);
gx=funcg(nodes);
hx=funch(nodes);
h=plot(nodes,1e14.*fx);
set(h,'linestyle','-','linewidth',1.5);hold on;
h=plot(nodes,1e14.*gx);
set(h,'linestyle','-','linewidth',1.5);hold on;
h=plot(nodes,1e14.*hx);
set(h,'linestyle','-','linewidth',1.5);hold on;
set(gca,'FontSize',20);
title('(x-1)^8 near 1.0(magnified by 1e14)');
legend = legend('{f(x)}','{g(x)}','{h(x)}');
axis([0.99,1.01,-2,2]);
set(legend,'FontSize',10,'FontName', 'Times New Roman');
saveas(1,'AssignmentA.png');
disp('Plot has been generated as AssignmentA.png.')
clf(1);

%Assignment_B
disp('------------------------Assignment B--------------------------');
beta=2;p=3;L=-1;U=1;
UFL=beta^L;OFL=beta^U*(beta-beta^(1-p));
fprintf('(1)UFL=%f,OFL=%f.\n',UFL,OFL);
num_nF=1;
for exp=L:U
normalF(num_nF)=beta^exp*(1);
normalF(num_nF+1)=beta^exp*(1+beta^(-2));
normalF(num_nF+2)=beta^exp*(1+beta^(-1));
normalF(num_nF+3)=beta^exp*(1+beta^(-1)+beta^(-2));
num_nF=num_nF+4;
end
normalF(num_nF)=0;
for i=1:num_nF-1
   normalF(num_nF+i)=-normalF(i); 
end
normalF=sort(normalF);
fprintf('(2)all normal numbers in F are listed, in a totol of %d:\n',length(normalF));
format shortG
disp(normalF');
hold on;
figure(2);
plot(normalF,zeros(length(normalF)),'bo','MarkerSize',5,'MarkerFaceColor','b');
set(gca,'XLim',[-4 4]);
set(gca,'XTick',[-4:1:4]);
set(gca,'XTickLabel',[-4:1:4]);
set(gca,'YLim',[-1 1]);
set(gca,'YTick',[-1:1:1]);
title('F');

fprintf('(3)Plot has been generated. \n');
subnormalF(1)=beta^L*(beta^(-2));
subnormalF(2)=beta^L*(beta^(-1));
subnormalF(3)=beta^L*(beta^(-1)+beta^(-2));
for i=1:3
   subnormalF(3+i)=-subnormalF(4-i); 
end
fprintf('(4)all subnormal numbers in F are listed, \n');
format shortG
disp(subnormalF');
hold on;
plot(subnormalF,zeros(length(subnormalF)),'go','MarkerSize',5,'MarkerFaceColor','g');
plot([-5 5],[0 0],'b');
saveas(2,'AssignmentB.png');
fprintf('(5)Plot has been generated as AssignmentB.png \n');
clf(2);

function y = funch(x)
y=(x-1).^8;
end

function y = funcf(x)
y=x.^8-8.*x.^7+28.*x.^6-56.*x.^5+70.*x.^4-56.*x.^3+28.*x.^2-8.*x+1;
end

function y = funcg(x)
y=(((((((x-8).*x+28).*x-56).*x+70).*x-56).*x+28).*x-8).*x+1;
end